import { ActionReducerMapBuilder, AsyncThunk, createAsyncThunk, createSlice, PayloadAction } from '@reduxjs/toolkit';
import type { Page, Role } from '@/type/RoleType';
import { addRoleAPI, deleteRoleAPI, fetchEnabledRoleListAPI, fetchRoleListAPI, findRoleAPI, updateRoleAPI } from '@/api/roleAPI';
import { AsyncThunkConfig } from '@reduxjs/toolkit/dist/createAsyncThunk';


type InitialState = {
  roleList: Role[],
  total: number,
  roleDetail: Role
};

const initialState: InitialState = {
  roleList: [],
  total: 0,
  roleDetail: {} as Role,
};


/**
 * @description 获取-分页查询角色列表的异步函数
 * */
export const fetchRoleList: any = createAsyncThunk('fetchRoleList', async (page: Page, { rejectWithValue }) => {
  try {
    const { data } = await fetchRoleListAPI(page);
    return data;
  } catch (error: any) {
    return rejectWithValue(error.message);
  }
});

/**
 * @description 删除-角色的异步函数
 * */
export const deleteRole: any = createAsyncThunk('deleteRole', async (id: number, { rejectWithValue }) => {
  try {
    const { message } = await deleteRoleAPI(id);
    return message;
  } catch (error: any) {
    return rejectWithValue(error.message);
  }
});

/**
 * @description 添加-角色的异步函数
 * */
export const addRole: any = createAsyncThunk('addRole', async (formData: Role, { rejectWithValue }) => {
  try {
    const { message } = await addRoleAPI(formData);
    return message;
  } catch (error: any) {
    return rejectWithValue(error.message);
  }
});


/**
 * @description 获取-角色详情的异步函数
 * */
export const findRole: any = createAsyncThunk('findRole', async (id: number, { rejectWithValue }) => {
  try {
    const { data } = await findRoleAPI(id);
    return data;
  } catch (error: any) {
    return rejectWithValue(error.message);
  }
});


/**
 * @description 修改-角色详情的异步函数
 * */
export const updateRole: any = createAsyncThunk('updateRole', async (formData: Role, { rejectWithValue }) => {
  try {
    const { message } = await updateRoleAPI(formData);
    return message;
  } catch (error: any) {
    return rejectWithValue(error.message);
  }
});


/**
 * @description 获取-已启用角色列表的异步函数
 * */
export const fetchEnabledRoleList: AsyncThunk<{ id: number, name: string }[], void, AsyncThunkConfig> = createAsyncThunk('fetchEnabledRoleList', async (_, { rejectWithValue }) => {
  try {
    const { data } = await fetchEnabledRoleListAPI();
    return data;
  } catch (error: any) {
    return rejectWithValue(error.message);
  }
});


const roleStore = createSlice({
  name: 'roleStore',
  initialState,
  reducers: {},
  extraReducers(builder: ActionReducerMapBuilder<InitialState>) {
    builder.addCase(fetchRoleList.fulfilled, (state: InitialState, action: PayloadAction<{ rows: Role[], total: number }>) => {
      const { payload } = action;
      state.roleList = payload.rows;
      state.total = payload.total;
    });
    builder.addCase(findRole.fulfilled, (state: InitialState, action: PayloadAction<Role>) => {
      const { payload } = action;
      state.roleDetail = payload;
    });
  },
});

export default roleStore.reducer;
